﻿using Microsoft.Xaml.Interactivity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace Demo02
{
    [Microsoft.Xaml.Interactivity.TypeConstraint(typeof(Page))]
    public class ResponsiveBehavior : DependencyObject, IBehavior
    {
        public Windows.UI.Xaml.DependencyObject AssociatedObject { get; set; }

        public void Attach(Windows.UI.Xaml.DependencyObject associatedObject)
        {
            this.AssociatedObject = associatedObject;
            Windows.UI.Xaml.Window.Current.SizeChanged += Current_SizeChanged;
        }

        public void Detach()
        {
            Windows.UI.Xaml.Window.Current.SizeChanged += Current_SizeChanged;
        }

        void Current_SizeChanged(object sender, Windows.UI.Core.WindowSizeChangedEventArgs e)
        {
            var control = this.AssociatedObject as Page;
            if (Window.Current.Bounds.Width <= this.NarrowWidth)
            {
                // narrow
                if (!string.IsNullOrEmpty(this.NarrowState))
                    VisualStateManager.GoToState(control, this.NarrowState, this.Transitions);
            }
            else
            {
                switch (Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().Orientation)
                {
                    // landscape
                    case Windows.UI.ViewManagement.ApplicationViewOrientation.Landscape:
                        if (!string.IsNullOrEmpty(this.LandscapeState))
                            VisualStateManager.GoToState(control, this.LandscapeState, this.Transitions);
                        break;
                    // portrait
                    case Windows.UI.ViewManagement.ApplicationViewOrientation.Portrait:
                        if (!string.IsNullOrEmpty(this.PortraitState))
                            VisualStateManager.GoToState(control, this.PortraitState, this.Transitions);
                        break;
                }
            }
        }

        [Microsoft.Xaml.Interactivity.CustomPropertyValueEditor(Microsoft.Xaml.Interactivity.CustomPropertyValueEditor.StateName)]
        public string LandscapeState { get { return (string)GetValue(LandscapeStateProperty); } set { SetValue(LandscapeStateProperty, value); } }
        public static readonly DependencyProperty LandscapeStateProperty = DependencyProperty.Register("LandscapeState", typeof(string), typeof(ResponsiveBehavior), null);

        [Microsoft.Xaml.Interactivity.CustomPropertyValueEditor(Microsoft.Xaml.Interactivity.CustomPropertyValueEditor.StateName)]
        public string PortraitState { get { return (string)GetValue(PortraitStateProperty); } set { SetValue(PortraitStateProperty, value); } }
        public static readonly DependencyProperty PortraitStateProperty = DependencyProperty.Register("PortraitState", typeof(string), typeof(ResponsiveBehavior), null);

        [Microsoft.Xaml.Interactivity.CustomPropertyValueEditor(Microsoft.Xaml.Interactivity.CustomPropertyValueEditor.StateName)]
        public string NarrowState { get { return (string)GetValue(NarrowStateProperty); } set { SetValue(NarrowStateProperty, value); } }
        public static readonly DependencyProperty NarrowStateProperty = DependencyProperty.Register("NarrowState", typeof(string), typeof(ResponsiveBehavior), null);

        public int NarrowWidth { get { return (int)GetValue(NarrowWidthProperty); } set { SetValue(NarrowWidthProperty, value); } }
        public static readonly DependencyProperty NarrowWidthProperty = DependencyProperty.Register("NarrowWidth", typeof(int), typeof(ResponsiveBehavior), new PropertyMetadata(320));

        public bool Transitions { get { return (bool)GetValue(TransitionsProperty); } set { SetValue(TransitionsProperty, value); } }
        public static readonly DependencyProperty TransitionsProperty = DependencyProperty.Register("Transitions", typeof(bool), typeof(ResponsiveBehavior), new PropertyMetadata(true));
    }
}
